package org.pine.service.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.pine.api.beans.request.PageRequest;
import org.pine.api.beans.response.PageResponse;

import java.util.List;

/**
 * 分页工具
 *
 * @author pine manage
 * @since 2024-08-09
 */
public class PageUtil {

    /**
     * 分页结构转换
     */
    public static <T> PageResponse<T> convertPage(IPage<?> page, List<T> list) {
        return new PageResponse<>(list, page.getTotal(),  page.getCurrent(), page.getSize());
    }

    /**
     * 请求参数转换,由请求体转换为query对象
     */
    public static <QueryRequest, Query> PageRequest<Query> convertQuery(PageRequest<QueryRequest> request, Query query) {
        PageRequest<Query> pageRequest = new PageRequest<>();
        pageRequest.setPageSize(request.getPageSize());
        pageRequest.setPageNum(request.getPageNum());
        pageRequest.setData(query);
        return pageRequest;
    }
}
